package com.parking.status.sync.schedule;

import com.parking.status.sync.entity.CommandBase;
import com.parking.status.sync.entity.ParkingLot;
import com.parking.status.sync.entity.ParkingStatus;
import com.parking.status.sync.netty.client.TcpClient;
import com.parking.status.sync.netty.entity.*;
import com.parking.status.sync.netty.service.CommandFactory;
import com.parking.status.sync.netty.service.RequestFactory;
import com.parking.status.sync.netty.util.ContentFactory;
import com.parking.status.sync.repository.ParkingLotRepository;
import com.parking.status.sync.repository.ParkingStatusRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Jianjun.Hu on 2015/9/11. 123
 */
@Service
public class FixRunner {

    private Logger logger = LoggerFactory.getLogger(FixRunner.class);

    private int count = 0;

    @Autowired
    private TcpClient tcpClient;

    @Autowired
    ParkingStatusRepository repo;

    @Autowired
    ParkingLotRepository repoParkingLot;

    @Autowired
    CommandFactory commandFactory;

    @Autowired
    RequestFactory requestFactory;

    @Scheduled(fixedRate = 5000000)
    public void run() {
        count++;
        logger.info("Run the {} times at {}.", count, new Date().toString());

//        try {
//            Thread.sleep(10 * 1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

//        List<ParkingLot> listParkingLot = repoParkingLot.findStatusEqualZero();
//
//        try {
//            for(ParkingLot parkingLot : listParkingLot) {
//
//                tcpClient.start();
//                ContentBase content = ContentFactory.getContent(parkingLot);
//                tcpClient.send(content);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            try {
//                tcpClient.stop();
//            } catch (Exception e1) {
//                e1.printStackTrace();
//            }
//        }
        try {
            tcpClient.start();

//            RequestSyncParkingInfo info = new RequestSyncParkingInfo();
//            RequestParkingCheckin info = new RequestParkingCheckin();
//
//            info.forTest();
            CommandBase command = commandFactory.createCommandFromDB();
            RequestPre info = requestFactory.createRequestByCommand(command);
            tcpClient.send(info);
        }catch (Exception e) {
             e.printStackTrace();
            try {
                tcpClient.stop();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        // fetch all to report
        // where status=0

        // report
        // tcp/ip send binary data

        // refresh data reported
        // update status=1
    }
}
